package servlet;

import java.io.IOException;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import connection.DbConnection;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.Calendar;
import java.util.StringTokenizer;

import model.EmailingSystem;
import model.PollUserImpl;
import bean.PollUser;

public class changePersonalDetailsServlet extends HttpServlet {

	
	/**
	 * 
	 */
	private static final long serialVersionUID = 1006091622807200265L;

	public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doPost (request, response);
	}
	
	public void doPost (HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		response.setContentType("text/html");
		response.setCharacterEncoding("GBK");
		
		System.out.println("changePersonalDetailsServlet() run");
		
		// Clear other success messages
		request.getSession().setAttribute("settings_success", "f");
		request.getSession().setAttribute("settings_incorrect_pw", "f");
		request.getSession().setAttribute("settings_add_device_success","f");
		request.getSession().setAttribute("settings_delete_device_success","f");
		
		// Get parameters
		String firstname = request.getParameter("first_name");
		String lastname = request.getParameter("last_name");
		String email = request.getParameter("email");
		
		// Error checking
		System.out.println("firstname: "+firstname);
		System.out.println("lastname: "+lastname);
		System.out.println("email: "+email);
		
		// Create poll user bean
		PollUser user = new PollUser();
		user.setUsername(request.getSession().getAttribute("username").toString());
		PollUserImpl.setUserAttributes(user);
		
		// Change details
		user.setFirstname(firstname);
		user.setLastname(lastname);
		user.setEmail(email);
		
		// Check bean
		System.out.println("user bean firstname: "+user.getFirstname());
		System.out.println("user bean lastname: "+user.getLastname());
		System.out.println("user bean email"+user.getEmail());
		
		// Update database
		DbConnection db = new DbConnection();
		String sql = "Update PollUser set fname='"+user.getFirstname()+"', lname='"+user.getLastname()+"', email='"+user.getEmail()+"' where username='"+user.getUsername()+"'";
		System.out.println("Update personal details SQL: "+sql);
		db.doUpdate(sql);
		
		// Change to new session variables
		request.getSession().setAttribute("fname", user.getFirstname());
		request.getSession().setAttribute("lname", user.getLastname());
		request.getSession().setAttribute("email", user.getEmail());
		
		// Set success session attribute
		request.getSession().setAttribute("settings_change_personal_success", "t");
		
		// Send back
		response.sendRedirect("settings.jsp");
	}
}
